package com.smartwebx.ha.web.util.kml;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import com.smartwebx.ha.util.IsoPoint;
import com.smartwebx.ha.util.IsoProperty;
import com.smartwebx.ha.util.IsoSurface;


public class GetIsoSurface {
	public IsoProperty readIsoSurface(String str) throws IOException{
		IsoProperty isoProperty = new IsoProperty();
		List<IsoSurface> rgbListInit= new ArrayList<>();
		IsoSurface isoSurface1 = new IsoSurface("00FFFF", "0", "122.79013458925772,33.298459903020252,122.66715857076402,33.298459903020252,122.66715857076402,33.371454577078772,122.79013458925772,33.371454577078772,122.79013458925772,33.298459903020252", "1");
		IsoSurface isoSurface2 = new IsoSurface("00B2EE", "0", "122.79013458925772,33.371454577078772,122.66715857076402,33.371454577078772,122.66715857076402,33.444449251137293,122.79013458925772,33.444449251137293,122.79013458925772,33.371454577078772", "1");
		IsoSurface isoSurface3 = new IsoSurface("0000FF", "0", "122.79013458925772,33.444449251137293,122.66715857076402,33.444449251137293,122.66715857076402,33.517443925195813,122.79013458925772,33.517443925195813,122.79013458925772,33.444449251137293", "1");
		IsoSurface isoSurface4 = new IsoSurface("EEDC82", "0", "122.79013458925772,33.517443925195813,122.66715857076402,33.517443925195813,122.66715857076402,33.590438599254334,122.79013458925772,33.590438599254334,122.79013458925772,33.517443925195813", "1");
		IsoSurface isoSurface5 = new IsoSurface("EE9A00", "0", "122.79013458925772,33.590438599254334,122.66715857076402,33.590438599254334,122.66715857076402,33.663433273312847,122.79013458925772,33.663433273312847,122.79013458925772,33.590438599254334", "1");
		IsoSurface isoSurface6 = new IsoSurface("EE30A7", "0", "122.79013458925772,33.663433273312847,122.66715857076402,33.663433273312847,122.66715857076402,33.736427947371368,122.79013458925772,33.736427947371368,122.79013458925772,33.663433273312847", "1");
		IsoSurface isoSurface7 = new IsoSurface("EE4000", "0", "122.79013458925772,33.736427947371368,122.66715857076402,33.736427947371368,122.66715857076402,33.809422621429889,122.79013458925772,33.809422621429889,122.79013458925772,33.736427947371368", "1");
		IsoSurface isoSurface8 = new IsoSurface("EE3B3B", "0", "122.79013458925772,33.809422621429889,122.66715857076402,33.809422621429889,122.66715857076402,33.882417295488409,122.79013458925772,33.882417295488409,122.79013458925772,33.809422621429889", "1");
		IsoSurface isoSurface9 = new IsoSurface("EE2C2C", "0", "122.79013458925772,33.882417295488409,122.66715857076402,33.882417295488409,122.66715857076402,33.95541196954693,122.79013458925772,33.95541196954693,122.79013458925772,33.882417295488409", "1");
		IsoSurface isoSurface10 = new IsoSurface("EE0000", "0", "122.79013458925772,33.95541196954693,122.66715857076402,33.95541196954693,122.66715857076402,34.028406643605443,122.79013458925772,34.028406643605443,122.79013458925772,33.95541196954693", "1");
		
		rgbListInit.add(isoSurface1);
		rgbListInit.add(isoSurface2);
		rgbListInit.add(isoSurface3);
		rgbListInit.add(isoSurface4);
		rgbListInit.add(isoSurface5);
		rgbListInit.add(isoSurface6);
		rgbListInit.add(isoSurface7);
		rgbListInit.add(isoSurface8);
		rgbListInit.add(isoSurface9);
		rgbListInit.add(isoSurface10);
		
		System.out.println(str);
		String str2 = str.replaceAll(":", ",").trim();
		String[] s1=str2.split("\\|");
		List<IsoSurface> isoSurfaceList= new ArrayList<IsoSurface>();
		MyComparator comparator = new MyComparator();
		Set<String> valSet = new TreeSet<>(comparator);
		List<IsoPoint> isoPoints = new ArrayList<>();
		for(String str1:s1){
			IsoPoint isoPoint = new IsoPoint();
			System.out.println(str1);
			String[] s2= str1.split(",dzm,");
			String[] points=s2[0].split(",");
			
			String[] valAndProperty= s2[1].split(",");
			IsoSurface isoSurface = new IsoSurface("",valAndProperty[0],s2[0],valAndProperty[1]);
			//数值标点
			isoPoint.setPoints(points[points.length-2]+","+points[points.length-1]);
			isoPoint.setPointVal(valAndProperty[0]);
			isoPoints.add(isoPoint);
			
			isoSurfaceList.add(isoSurface);
			valSet.add(valAndProperty[0]);
		}
		List<IsoSurface> isoColorCode = new ArrayList<>();
		int i=1;
		for(Iterator<String> iterator  = valSet.iterator();iterator.hasNext();){
			String val=iterator.next();
			rgbListInit.get(i).setLineVal(val);
			isoColorCode.add(rgbListInit.get(i));
			IsoPoint isoPoint = new IsoPoint();
			String[] colorCodes= rgbListInit.get(i).getPoints().split(",");
			isoPoint.setPoints(colorCodes[colorCodes.length-2]+","+colorCodes[colorCodes.length-1]);
			isoPoint.setPointVal(val);
			isoPoints.add(isoPoint);
			i++;
		}
		System.out.println(isoSurfaceList);
		for(int j=0;j<isoSurfaceList.size();j++){
			for(int m=0;m<isoColorCode.size();m++){
				String areaLineVal,areaLineProperty,codeLineVal;
				areaLineVal=isoSurfaceList.get(j).getLineVal();
				areaLineProperty=isoColorCode.get(m).getLineVal();
				codeLineVal=isoSurfaceList.get(j).getProperty();
				if(areaLineVal.equals(areaLineProperty) && codeLineVal.equals("1")){
					isoSurfaceList.get(j).setAreaColor(isoColorCode.get(m).getAreaColor());
				}else if(areaLineVal.equals(areaLineProperty) && codeLineVal.equals("-1")){
					if(m==0){
						isoSurfaceList.get(j).setAreaColor("00FFFF");
					}else{
						isoSurfaceList.get(j).setAreaColor(isoColorCode.get(m-1).getAreaColor());
					}
					
				}
				System.out.println(j);
			}
		}
		isoColorCode.addAll(isoSurfaceList);
		isoProperty.setIsoSurfaces(isoColorCode);
		isoProperty.setIsoPoints(isoPoints);
		return isoProperty;
	}
	
	public static class MyComparator implements Comparator<String>{

		@Override
		public int compare(String o1, String o2) {
			String compare1=o1.substring(0, o1.indexOf("."));
			String compare2=o2.substring(0, o2.indexOf("."));
			if(Integer.parseInt(compare1)>Integer.parseInt(compare2)){
				return 1;
			}else if(Integer.parseInt(compare1)==Integer.parseInt(compare2)){
				return 0;
			}else{
				return -1;
			}
		}
	}
}
